package com.xyazm.order.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyazm.common.dao.BaseDAO;
import com.xyazm.order.domain.dto.AfterSaleOrderListDTO;
import com.xyazm.order.domain.dto.CustomerAuditResultDTO;
import com.xyazm.order.domain.dto.QueryAfterSaleListDTO;
import com.xyazm.order.domain.entity.AfterSaleInfoDO;
import com.xyazm.order.mapper.AfterSaleInfoMapper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 订单售后表
 * @Author: xuyou
 * @Date: 2022/6/29 14:29
 */
@Repository
public class AfterSaleInfoDAO extends BaseDAO<AfterSaleInfoMapper, AfterSaleInfoDO> {

    /**
     * 根据售后单号查询售后单
     * @author xuyou
     * @date: 2022/7/1 16:56
     */
    public AfterSaleInfoDO getOneByAfterSaleId(String afterSaleId) {
        LambdaQueryWrapper<AfterSaleInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AfterSaleInfoDO::getAfterSaleId, afterSaleId);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 更新售后单状态
     * @author xuyou
     * @date: 2022/7/1 17:04
     */
    public boolean updateStatus(String afterSaleId, Integer fromStatus, Integer toStatus) {
        LambdaUpdateWrapper<AfterSaleInfoDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(AfterSaleInfoDO::getAfterSaleId, afterSaleId)
                .eq(AfterSaleInfoDO::getAfterSaleStatus, fromStatus)
                .set(AfterSaleInfoDO::getAfterSaleStatus, toStatus);
        return update(updateWrapper);
    }


    /**
     * 客服审核售后结果更新售后单信息
     * @author xuyou
     * 2022/7/20 0020 21:56
     * @param afterSaleStatus
     * @param customerAuditAssembleRequest
    */
    public void updateCustomerAuditAfterSaleResult(Integer afterSaleStatus, CustomerAuditResultDTO customerAuditAssembleRequest) {
        String afterSaleId = customerAuditAssembleRequest.getAfterSaleId();
        String reviewReason = customerAuditAssembleRequest.getReviewReason();
        Integer reviewReasonCode = customerAuditAssembleRequest.getReviewReasonCode();
        Integer reviewSource = customerAuditAssembleRequest.getReviewSource();
        Date reviewTime = customerAuditAssembleRequest.getReviewTime();

        UpdateWrapper<AfterSaleInfoDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("after_sale_status", afterSaleStatus);
        updateWrapper.set(reviewReason != null, "review_reason", reviewReason);
        updateWrapper.set(reviewReasonCode != null, "review_reason_code", reviewReasonCode);
        updateWrapper.set(reviewSource != null, "review_source", reviewSource);
        updateWrapper.set(reviewTime != null, "review_time", reviewTime);
        updateWrapper.eq("after_sale_id", afterSaleId);
        update(updateWrapper);
    }

    /**
     * 查询售后列表
     * @author xuyou
     * @date: 2022/7/21 16:44
     * @param: queryDTO
     */
    public Page<AfterSaleOrderListDTO> listByPage(QueryAfterSaleListDTO query) {
        Page<AfterSaleOrderListDTO> page = new Page<>(query.getPageNo(), query.getPageSize());
        return baseMapper.listByPage(page, query);
    }

    /**
     * 根据订单id查询售后类型为缺品的售后单
     * @author xuyou
     * @date: 2022/7/21 17:11
     */
    public List<AfterSaleInfoDO> listByOrderIdAndAfterSaleType(String orderId, Integer afterSaleType) {
        LambdaQueryWrapper<AfterSaleInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AfterSaleInfoDO::getOrderId, orderId)
                .eq(AfterSaleInfoDO::getAfterSaleTypeDetail, afterSaleType);
        return list(queryWrapper);
    }
}
